package com.example.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.common.HttpStatusConstants;
import com.example.common.R;
import com.example.mapper.CategoryMapper;
import com.example.mapper.ConCategoryServiceMapper;
import com.example.mapper.ServiceMapper;
import com.example.model.HomeServiceItemDTO;
import com.example.model.req.AddServiceReq;
import com.example.model.resp.HomeListServiceResp;
import com.example.model.resp.ListServiceResp;
import com.example.service.ServiceService;

@Service
public class ServiceServiceImpl implements ServiceService {
    @Autowired
    private ServiceMapper serviceMapper;
    @Autowired
    private ConCategoryServiceMapper conCategoryServiceMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public R<List<ListServiceResp>> listServiceByCategory(Integer category) {
        List<Integer> serviceIds = conCategoryServiceMapper.getServiceListByCategoryId(category);
        List<ListServiceResp> respList = serviceMapper.getServicesByIds(serviceIds).stream()
                .map(service -> ListServiceResp.builder()
                        .serviceId(service.getId())
                        .pic(service.getPic())
                        .serviceName(service.getServiceName())
                        .description(service.getDescription())
                        .build())
                .collect(Collectors.toList());
        return R.success(respList);
    }

    @Override
    public R<String> addService(AddServiceReq addServiceReq) {
        try {
            serviceMapper.addService(addServiceReq);
            Integer serviceId = addServiceReq.getId();
            conCategoryServiceMapper.addCategoryService(addServiceReq.getCategoryId(), serviceId);
            return R.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(HttpStatusConstants.INTERNAL_SERVER_ERROR, "");
        }
    }

    @Override
    public R<String> deleteService(Integer id) {
        try {
            serviceMapper.detelteService(id);
            conCategoryServiceMapper.deleteCategoryServiceByServiceId(id);
            return R.success("删除成功");
        } catch (Exception e) {
            return R.error(HttpStatusConstants.INTERNAL_SERVER_ERROR, "删除失败, 请重试");
        }
    }

    @Override
    public R<List<HomeListServiceResp>> getHomeListServiceAllCategory() {
        List<HomeListServiceResp> respList = categoryMapper.getHomeCategoryList().stream()
                .map(homeCategory -> {
                    List<HomeServiceItemDTO> serviceList = conCategoryServiceMapper
                            .getHomeServiceListByCategoryId(homeCategory.getId());
                    return HomeListServiceResp.builder()
                            .categoryId(homeCategory.getId())
                            .categoryName(homeCategory.getCategory())
                            .serviceList(serviceList)
                            .build();
                })
                .collect(Collectors.toList());
        return R.success(respList);
    }
}
